package com.engine.jucailinkq.attendance.attendanceanalysis.cmd.item;

import com.engine.jucailinkq.attendance.enums.AttendanceItemTypeEnum;
import com.engine.jucailinkq.attendance.enums.CheckBoxEnum;
import com.engine.jucailinkq.attendance.enums.WorkForTimeEnum;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 人员旷工项目
 */
@Slf4j
public class AbsenteeismItemCmd extends AbstractCommonCommand<Map<String,Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public AbsenteeismItemCmd(Map<String,Object> params){
        this.params=params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        //作用时段
        String workfor = Util.null2String(params.get("workfor"));
        //旷工分钟数
        int time = Util.null2String(params.get("time")).equals("")? 0:Util.getIntValue(Util.null2String(params.get("time")));
        //漏打卡转旷工标识
        String forgetConvertAbsenteeism = Util.null2String(params.get("workfor"));
        Map<String, Object> resultMap = new HashMap<>();

        List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)params.get("attendanceItems");
        attendanceItems = attendanceItems.stream().filter(e -> {
            //项目类型
            String xmlx = Util.null2String(e.get("xmlx"));
            //作用时段
            String zysd = Util.null2String(e.get("zysd"));
            //检查漏打卡情况
            String ldkzkg = Util.null2String(e.get("ldkzkg"));
            //最小核算分钟数（不包含）
            int zxhsl = Util.null2String(e.get("zxhsl")).equals("")?0:Util.getIntValue(Util.null2String(e.get("zxhsl")));
            //最大核算分钟数（包含）
            int zdhsl = Util.null2String(e.get("zdhsl")).equals("")?0:Util.getIntValue(Util.null2String(e.get("zdhsl")));
            if (CheckBoxEnum.CHECKED.getKey().equals(forgetConvertAbsenteeism)){
                //漏打卡转旷工
                if(ldkzkg.equals(CheckBoxEnum.CHECKED.getKey()) && AttendanceItemTypeEnum.ABSENTEESIM.getKey().equals(xmlx) && (zysd.contains(workfor) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())) && time > zxhsl && time<=zdhsl) {
                    return true;
                }else if (ldkzkg.equals(CheckBoxEnum.CHECKED.getKey()) && AttendanceItemTypeEnum.ABSENTEESIM.getKey().equals(xmlx) && (zysd.contains(workfor) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())) && zxhsl==0 && zdhsl==0){
                    return true;
                }else {
                    return false;
                }
            }else {
                if(AttendanceItemTypeEnum.ABSENTEESIM.getKey().equals(xmlx) && (zysd.contains(workfor) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())) && time > zxhsl && time<=zdhsl) {
                    return true;
                }else if (AttendanceItemTypeEnum.ABSENTEESIM.getKey().equals(xmlx) && (zysd.contains(workfor) || zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())) && zxhsl==0 && zdhsl==0){
                    return true;
                }else {
                    return false;
                }
            }
        }).collect(Collectors.toList());
        log.debug("absenteeismCmd attendanceItems : [{}]",attendanceItems);
        resultMap.put("attendanceItems",attendanceItems);

        return resultMap;
    }
}
